package com.example.musicplayer.controller;

import com.example.musicplayer.mapper.LoveMusicMapper;
import com.example.musicplayer.model.Music;
import com.example.musicplayer.model.User;
import com.example.musicplayer.tools.Constant;
import com.example.musicplayer.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {
    @Autowired
    private LoveMusicMapper loveMusicMapper;

    @RequestMapping("/likemusic")
    public ResponseBodyMessage<Boolean> insertLoveMusic(HttpServletRequest request,
                                                        @RequestParam String id) {
        // 检查是否登录
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null){
            return new ResponseBodyMessage<>(0, "请登录后上传", false);
        }
        /*
            注意, userId可以通过session获取, 不用前端传参
         */
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();

        // 检查是否重复收藏
        Music music = loveMusicMapper.findLoveMusic(userId, Integer.parseInt(id));
        if (music != null){
            return new ResponseBodyMessage<>(0, "请勿重复收藏", false);
        }

        // 插入音乐
        if (loveMusicMapper.insertLoveMusic(userId, Integer.parseInt(id))){
            return new ResponseBodyMessage<>(1, "收藏成功", true);
        }
        return new ResponseBodyMessage<>(0, "收藏失败", false);
    }

    @RequestMapping("/findlovemusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(
            @RequestParam(required = false) String musicName,
            HttpServletRequest request){
        // 检查是否登录
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null){
            return new ResponseBodyMessage<>(0, "请登录后查找", null);
        }

        //获取UserId
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();

        //检查是否传参
        List<Music> musicList = null;
        if (musicName == null){
            musicList = loveMusicMapper.findLoveMusicById(userId);
        }else {
            musicList = loveMusicMapper.findLoveMusicByNameAndId(musicName, userId);
        }
        return new ResponseBodyMessage<>(1, "查询到了所有的歌曲信息", musicList);
    }

    @RequestMapping("/deletelovemusic")
    public ResponseBodyMessage<Boolean> deleteLoveMusic(
            HttpServletRequest request,
            @RequestParam String id){
        // 检查是否登录
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null){
            return new ResponseBodyMessage<>(0, "请登录后查找", null);
        }

        // 获取UserId
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();

        int musicId = Integer.parseInt(id);
        boolean reg = loveMusicMapper.deleteLoveMusic(userId, musicId);
        if (reg){
            return new ResponseBodyMessage<>(1, "取消收藏音乐成功", true);
        }
        return new ResponseBodyMessage<>(0, "取消收藏音乐失败", false);
    }
}
